package com.secure.orm.controller.billPay;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.secure.common.util.PayUtil;
import com.secure.orm.controller.BaseController;
import com.secure.orm.enums.PayType;
import com.secure.orm.enums.ThirdPayNotifyType;
import com.secure.orm.model.response.ViewData;
import com.secure.orm.service.billPay.NotifyService;
import com.secure.orm.thirdPay.alipay.Alipay;

@Controller
@RequestMapping("/third")
public class NotifyController extends BaseController {
	protected Log logger = LogFactory.getLog(NotifyController.class);
	
	@Autowired
	private NotifyService notifyService;

	@RequestMapping(value = "alipay/notify")
	public @ResponseBody String notify(HttpServletRequest req, HttpServletResponse resp) {
		Map<String, String> params = PayUtil.requestParams2Map(req);
		logger.info(("NotifyAlipay" + PayType.Alipay.name() + ", orderId:" + params.get(Alipay.ALIPAY_OUT_TRADE_NO) + ", tradeStatus:" + params.get(Alipay.ALIPAY_TRADE_STATUS)));
		ViewData vd = null;
		try {
			vd = notifyService.handleNotify(params,PayType.Alipay,ThirdPayNotifyType.NOTIFY);
		} catch (Exception e) {
			logger.error("NotifyAlipay" + e);
			return Alipay.NOTIFY_SUCCESS_RESULT;
		}
		return vd.getMsg();
	}

}
